<?php
    //db settings page
    //require_once("db-settings.php"); //Require DB connection
    class User {
        public $f;
        public function __contruct($fname) {
            $this -> f = $fname;
        }
    }

    function sanitize($str)
    {
    	return strip_tags(trim(($str)));
    }
    //Generate a unique code
    function getUniqueCode($length = "")
    {
    	$code = md5(uniqid(rand(), true));
    	if ($length != "") return substr($code, 0, $length);
    	else return $code;
    }
    function createSession($sessionArray) {
        session_name('dyh_session');
        session_start(); //start a session
        //save data in session for later use
        $_SESSION['fname'] = $sessionArray['fname'];
        $_SESSION['lname'] = $sessionArray['lname'];
        $_SESSION['city'] = $sessionArray['city'];
        $_SESSION['country'] = $sessionArray['country'];
        $_SESSION['email'] = $sessionArray['email'];
        $_SESSION['age'] = $sessionArray['age'];
        $_SESSION['phone'] = $sessionArray['phone'];
    }

    function redirect() {
        header('location:page3.php');
    }

    if(!empty($_POST)) {    //if form submit
        //sanitize input data
        try {
            $fname = sanitize($_POST['firstname']);
            $lname = sanitize($_POST['lastname']);
            $city = sanitize($_POST['city']);
            $country = sanitize($_POST['country']);
            $email = sanitize($_POST['email']);
            $age = sanitize($_POST['age']);
            $phone = sanitize($_POST['telephone']);

            $arr = array('fname'=>$fname, 'lname'=>$lname, 'city'=>$city, 'country'=>$country, 'email'=>$email, 'age'=>$age, 'phone'=>$phone);
            createSession($arr);
            //create a user object
            //$user = new User('Joseph');
            //echo $user -> f;
            redirect();
        } catch(Exception $e) {
            echo 'Something went wrong';
        }
    }
?>